package com.baosight.risk.service.xtri.impl;

import java.util.List;
import java.util.Map;

import com.baosight.risk.common.utils.IDGenerator;
import com.baosight.risk.mapper.xtaa.code.XtaaCodeMapper;
import com.baosight.risk.service.xtaa.code.entity.XtaaCode;
import com.baosight.risk.service.xtri.entity.TXtriTempSystem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baosight.risk.api.xtri.IXtriTempRiskService;
import com.baosight.risk.base.mapper.BaseMapper;
import com.baosight.risk.base.service.BaseServiceImpl;
import com.baosight.risk.common.bean.BeanUtils;
import com.baosight.risk.common.utils.HttpClientUtil;
import com.baosight.risk.mapper.xtri.TXtriTempRiskMapper;
import com.baosight.risk.service.xtri.entity.TXtriTempRisk;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
@Service("xtriTempRiskServiceImpl")
public class XtriTempRiskServiceImpl extends BaseServiceImpl<TXtriTempRisk> implements IXtriTempRiskService {
    @Autowired
    private TXtriTempRiskMapper tempRiskMapper;

	@Autowired
	private XtaaCodeMapper xtaaCodeMapper;
    
    @Override
    public BaseMapper getMapper() {
        return tempRiskMapper;
    }
    
	@Override
	@Transactional
	public void importRisk() throws Exception {
		XtaaCode codeParam=new  XtaaCode();
		codeParam.setDdTypeCode("XTRI_TEMP_URL");
		codeParam.setDdCode("QUERY_SJ_3");
		List<XtaaCode> codeList=xtaaCodeMapper.select(codeParam);
		if(codeList.size()>0){
			String url = codeList.get(0).getDdDesc();
			String result = HttpClientUtil.doPostJson(url,"");
			JSONObject js = JSONObject.fromObject(result);

			if ("1".equals(js.getString("status"))) {
				tempRiskMapper.delete(new TXtriTempRisk());

				JSONArray jSONArray = js.getJSONArray("result");
				for(int i=0;i<jSONArray.size();i++){
					Map m = jSONArray.getJSONObject(i);
					TXtriTempRisk tempRisk = BeanUtils.map2Object(m, TXtriTempRisk.class);
					this.save(tempRisk);
				}
			}else{
				throw new 	Exception(js.getString("msg"));
			}
		}

	}

	@Override
	public TXtriTempRisk save(TXtriTempRisk tempRisk) {
		if("继续适用".equals(tempRisk.getFileStatus())){
			tempRisk.setFileStatusName(tempRisk.getFileStatus());
			tempRisk.setFileStatus("10");
		}else if("修订".equals(tempRisk.getFileStatus())){
			tempRisk.setFileStatusName(tempRisk.getFileStatus());
			tempRisk.setFileStatus("20");
		}else if("作废".equals(tempRisk.getFileStatus())){
			tempRisk.setFileStatusName(tempRisk.getFileStatus());
			tempRisk.setFileStatus("30");
		}else if("新增".equals(tempRisk.getFileStatus())){
			tempRisk.setFileStatusName(tempRisk.getFileStatus());
			tempRisk.setFileStatus("40");
		}
		try{
			tempRisk = (TXtriTempRisk)BeanUtils.initBean(tempRisk);
			tempRiskMapper.insertTempRisk(tempRisk);
		}catch (Exception e){
			e.printStackTrace();
		}

		return tempRisk;
	}

	@Override
	public List<TXtriTempRisk> queryTempRisk(TXtriTempRisk param) {
		servicePage();
		return tempRiskMapper.selectRisk(param);
	}

}
